import tools from "@/common/tools.js";
import {
	post
} from '@/common/axios.js';
import {
	getmomentsList, //动态列表
	getmomentsDetail, //动态详情
	addComment, //动态评论
	likeMoments, //动态详情点赞
	postMoments ,//发布动态
	
} from '@/common/path.js'
export default {
	namespaced: true,
	state: {
		dynamicList: [], //动态列表
		dynamicData: {},
		userInfo: uni.getStorageSync('userInfo')
	},
	getters: {

	},
	mutations: {
		SET_DYNAMIC_LIST(state, data) { //动态列表
			state.dynamicList = data
		},
		SET_DYNAMIC_DATA(state, data) { //动态详情+评论列表
			state.dynamicData = data
		},
	},
	actions: {
		//发布动态
		postMoments({
			commit,
			dispatch
		}, args) {
			post(postMoments, args).then(res => {
				if (res.code == 0) {
					uni.showModal({
						title: '温馨提示',
						content: '已提交，请耐心等待审核！',
						success: function(Res) {
							if (Res.confirm) {
								tools.back()
							} else if (Res.cancel) {
								tools.back()
							}
						}
					})
				} else {
					tools.showToast(res.msg)
				}
			})
		},

		//动态详情点赞
		likeMomentsApi({
			commit,
			state,
			dispatch
		}, args) {
			post(likeMoments, args).then(res => {
				if (res.code == 0) {
					tools.showToast(res.msg)
				} else {
					tools.showToast(res.msg)
				}
			})
		},

		//动态评论
		addCommentApi({
			commit,
			state,
			dispatch
		}, args) {
			if (args.content) {
				post(addComment, args).then(res => {
					if (res.code == 0) {
						tools.showToast2(res.msg)
						setTimeout(r => {
							uni.pageScrollTo({
								scrollTop: 810, // 设置一个超大值，以保证滚动条滚动到底部
								duration: 100
							})
						}, 100)

					} else {
						tools.showToast(res.msg)
					}
				})
			} else {
				tools.showToast('请发布有效内容')
			}
		},

		//动态列表
		getmomentsApi({
			dispatch,
			commit,
			state
		}, args) {
			post(getmomentsList, args).then(res => {
				if (res.code == 0) {
					if (args.pageNum == 1) {
						commit('SET_DYNAMIC_LIST', res.data)
						// console.log('[res.data]', res.data);
					} else {
						state.dynamicList.push(...res.data)
						commit('SET_DYNAMIC_LIST', state.dynamicList)
					}

				} else {
					commit('SET_DYNAMIC_LIST', [])
				}
			})
		},
		//动态详情
		getmomentsDetail({
			dispatch,
			commit,
			state
		}, args) {
			post(getmomentsDetail, args).then(res => {
				if (res.code == 0) {
					commit('SET_DYNAMIC_DATA', res.data)
				} else {
					commit('SET_DYNAMIC_DATA', {})
				}
			})
		}


	},
}
